﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BallCollider : MonoBehaviour {

    public static BallCollider Instance;

    public Text text;
    
    public AudioClip BallCrashClip;
    public AudioClip JumpSceneClip;
    public AudioClip[] SpaceClip;
    public GameObject[] Light;
    public GameObject[] SpacePic;
    [HideInInspector]
    public string name;

    private AudioSource audios;
    private AudioSource SpaceAudios;

    private void Start()
    {
        Instance = this;
        audios = Camera.main.GetComponent<AudioSource>();
        SpaceAudios = GameObject.Find("EventSystem").GetComponent<AudioSource>();
    }

    private void OnCollisionEnter(Collision collision)
    {
        //撞墙
        audios.clip = BallCrashClip;
        audios.Play();
        audios.loop = false;
    }

    /// <summary>
    /// 跳转场景音效
    /// </summary>
    /// <returns></returns>
    IEnumerator PlayJumpScene()
    {
        yield return new WaitForSeconds(0);
        audios.clip = JumpSceneClip;
        audios.Play();
        audios.loop = false;
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.name== "NorthAtlanticOcean")
        {
            RollBall.Instance.canRoll = false;
            Light[1].SetActive(true);
            SpacePic[1].SetActive(true);
            StartCoroutine("PlayJumpScene");
            StartCoroutine("JumpInterface",1); 
            name = "NorthAtlanticOcean";
        }
        else if (other.gameObject.name == "EgyptianPyramids")
        {
            Light[3].SetActive(true);
            SpacePic[3].SetActive(true);
            RollBall.Instance.canRoll = false;
            StartCoroutine("PlayJumpScene");
            Level6BeginEnd.Instance.EndGame();
        }
        else if (other.gameObject.name == "SweetVeld")
        {
            RollBall.Instance.canRoll = false;
            Light[2].SetActive(true);
            SpacePic[2].SetActive(true);
            StartCoroutine("PlayJumpScene");
            StartCoroutine("JumpInterface", 2);
            name = "SweetVeld";
        }
        else if (other.gameObject.name == "AmazonRainforest")
        {
            RollBall.Instance.canRoll = false;
            Light[0].SetActive(true);
            SpacePic[0].SetActive(true);
            StartCoroutine("PlayJumpScene");
            StartCoroutine("JumpInterface", 3);
            name = "AmazonRainforest";
        }
    }

    IEnumerator JumpInterface(int a)
    {
        yield return new WaitForSeconds(0.5f);
        if (a==1)
        {
            SpaceAudios.clip = SpaceClip[0];
            SpaceAudios.Play();
            SpaceAudios.loop = true;
            Light[1].SetActive(false);  
        }
        else if (a==2)
        {
            SpaceAudios.clip = SpaceClip[1];
            SpaceAudios.Play();
            SpaceAudios.loop = true;
            Light[2].SetActive(false);
        }
        else if (a == 3)
        {
            SpaceAudios.clip = SpaceClip[2];
            SpaceAudios.Play();
            SpaceAudios.loop = true;
            Light[0].SetActive(false);
        }

        GameObject.Find("GroundPlane").GetComponent<AudioSource>().Stop();
        Instantiate(Resources.Load("Prefabs/UI/TransformSpace"),GameObject.Find("Canvas").transform);
    }
}
